package net.NingXiaRuiTai.ShortChain.controller;


import net.NingXiaRuiTai.ShortChain.controller.request.LinkGroupAddRequest;
import net.NingXiaRuiTai.ShortChain.controller.request.LinkGroupUpdateRequest;
import net.NingXiaRuiTai.ShortChain.enums.BizCodeEnum;
import net.NingXiaRuiTai.ShortChain.model.VO.LinkGroupVO;
import net.NingXiaRuiTai.ShortChain.service.LinkGroupService;
import net.NingXiaRuiTai.ShortChain.utils.JsonData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yuan
 * @since 2025-02-16
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {
    @Resource
    private LinkGroupService linkGroupService;
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest addRequest){
        int rows= linkGroupService.add(addRequest);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }
    /**
     * 根据id删除分组
     * @param groupId   通过组id来删除数据
     * @return JsonData
     * */
    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id") Long groupId){
       int rows= linkGroupService.del(groupId);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);

    }
    /**
     * 根据id找详情
     * @param id 通过组id来查询到详情
     * @return JsonData
     */
    @GetMapping("/detail/{id}")
    public JsonData detail( @PathVariable("id") Long id){
        LinkGroupVO linkGroupVO = linkGroupService.detail(id);


        return JsonData.buildSuccess(linkGroupVO);
    }
    /**
     * 用户全部分组列表
     * @return JsonData
     */
    @GetMapping("/list")

    public JsonData findUserAllLinkGroup(){

        List<LinkGroupVO> list=linkGroupService.listAllGroup();

        return JsonData.buildSuccess(list);
    }
@PutMapping("/update")
    public JsonData updateById(@RequestBody LinkGroupUpdateRequest upDateRequest){
        int rows=linkGroupService.updateById(upDateRequest);

        return rows==1?JsonData.buildSuccess("更新成功"):JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }



}

